package com.wc.AlgoOJ_homework.ALgoOJ_BL5.L_合并区域;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/10 21:59
 * @description http://43.138.190.70:8888/p/1123?tid=66120c2c07c5c018a5dcf262
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110;
    static int[][] a = new int[N >> 1][N >> 1];
    static int[][] b = new int[N >> 1][N >> 1];
    // 用来模拟,大的反转
    static int[][] g = new int[N][N];
    static int[] dx = new int[]{-1, 1, 0, 0};
    static int[] dy = new int[]{0, 0, -1, 1};
    static int n;
    // 边界
    static int u, d, l, r;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                b[i][j] = sc.nextInt();
            }
        }
        // 初始化大正方形的边界
        l = u = 1;
        r = d = n << 1;
        int res = work1(a, b);
        res = Math.max(res, work2(b, a));
        out.println(res);
        out.flush();
    }

    // a x
    // b x   x 是 0矩阵， 大矩阵形状
    static int work1(int[][] a, int[][] b) {
        int res = 0;
        for (int m = 1; m <= 4; m++) {
            for (int k = 1; k <= 4; k++) {
                for (int move = 0; move <= n; move++) {
                    // 移动move步, a在上面动，b在左下角

                    // 填充
                    for (int i = 1; i <= n; i++) {
                        for (int j = 1; j <= n; j++) {
                            g[i][j + move] = a[i][j];
                        }
                    }
                    for (int i = 1; i <= n; i++) {
                        for (int j = 1; j <= n; j++) {
                            g[i + n][j] = b[i][j];
                        }
                    }

                    // 遍历
                    for (int i = u; i <= d; i++) {
                        for (int j = l; j <= r; j++) {
                            if (g[i][j] == 1) {
                                res = Math.max(res, dfs(i, j));
                            }
                        }
                    }
                }
                a = roll(a);
            }
            b = roll(b);
        }
        return res;
    }

    // a x
    // x b  x 是 0矩阵，大矩阵形状
    static int work2(int[][] a, int[][] b) {
        int res = 0;
        for (int m = 1; m <= 4; m++) {
            for (int k = 1; k <= 4; k++) {
                for (int move = 0; move <= n; move++) {
                    // 移动move步, a在上面动，b在右下角


                    // 填充大矩阵
                    for (int i = 1; i <= n; i++) {
                        for (int j = 1; j <= n; j++) {
                            g[i][j + move] = a[i][j];
                        }
                    }
                    for (int i = 1; i <= n; i++) {
                        for (int j = 1; j <= n; j++) {
                            g[i + n][j + n] = b[i][j];
                        }
                    }

                    for (int i = u; i <= d; i++) {
                        for (int j = l; j <= r; j++) {
                            if (g[i][j] == 1) {
                                res = Math.max(res, dfs(i, j));
                            }
                        }
                    }
                }
                a = roll(a);
            }
            b = roll(b);
        }
        return res;
    }

    static int dfs(int x, int y) {
        int res = 1;
        g[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (!isBorder(nx, ny) && g[nx][ny] == 1) {
                res += dfs(nx, ny);
            }
        }
        return res;
    }

    // 判断边界
    static boolean isBorder(int x, int y) {
        return x < u || x > d || y < l || y > r;
    }

    // 旋转90度
    static int[][] roll(int[][] a) {
        int[][] b = new int[n + 1][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                b[j][n - i + 1] = a[i][j];
            }
        }
        return b;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
